package com.taotao.protal.controller;

import com.taotao.common.utils.JsonUtils;
import com.taotao.content.service.ContentService;
import com.taotao.pojo.TbContent;
import com.taotao.protal.controller.pojo.Ad1Node;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

/**
 * 展示页面
 */
@Controller
public class PageController {

    //配置文件属性注入
    @Value("${AD1_CATEGORY}")
    private Long categoryId;

    @Autowired
    private ContentService contentService;


    /**
     * 展示首页数据
     *
     * @return
     */
    //接收URL的请求http://localhost:8082/index.html
    @RequestMapping("/index")
    public String showIndex(Model model) {
        //响应到jsp(Springmvc配置了视图管理器)

        List<TbContent> contentList = contentService.getContentListByCatId(categoryId);
        //查询出数据后封装到各自的pojo,例如此处为banner的pojo
        List<Ad1Node> ad1List = new ArrayList<>();
        for (TbContent tbContent : contentList) {
            Ad1Node ad1Node = new Ad1Node();
            ad1Node.setAlt(tbContent.getSubTitle());
            ad1Node.setHeight("600");
            ad1Node.setHeightB("400");
            ad1Node.setHref(tbContent.getUrl());
            ad1Node.setSrc(tbContent.getPic());
            ad1Node.setSrcB(tbContent.getPic2());
            ad1Node.setWidth("600");
            ad1Node.setWidthB("400");
            ad1List.add(ad1Node);
        }
        //EasyUI需要json数据
        model.addAttribute("ad1", JsonUtils.objectToJson(ad1List));
        return "index";
    }
}
